﻿using System;
using System.IO;
using System.IO.Compression;

namespace yacomp.algo
{
    using yacomp.helpers;
    using yacomp.os;
    internal class gzip_algo : api_compress_algo
    {
        public string tag { get { return "gzip"; } }

        public uint compress(YacompFile file, Stream dst)
        {
            if (file.info.compress_tag)
            {
                file.info.compress_mem.Position = file.info.data_offset;
                helper.ReadToMemory(file.info.compress_mem, dst, file.info.compress_size);
                return file.info.compress_size;
            }

            var ix = dst.Position;
            using (var mem = file.ToStream())
            using (var df = new GZipStream(dst, CompressionMode.Compress, true)) helper.ReadAllToMemory(mem, df);
            return (uint)(dst.Position - ix);
        }

        public void decompress(YacompFile file, Stream dst)
        {
            file.info.compress_mem.Position = file.info.data_offset;
            using (var df = new GZipStream(file.info.compress_mem, CompressionMode.Decompress, true)) helper.ReadToMemory(df, dst, file.info.not_compress_size);
        }
    }
}
